package com.design.YiTong.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.YiTong.common.exception.YyghException;
import com.design.YiTong.common.result.ResultCodeEnum;
import com.design.YiTong.model.user.UserInfo;
import com.design.YiTong.user.mapper.UserInfoMapper;
import com.design.YiTong.user.service.UserInfoService;
import com.design.YiTong.vo.user.LoginVo;
import com.design.YiTong.vo.user.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoServiceImpl extends
        ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public Map<String, String> loginUser(LoginVo loginVo) {
        //从loginVo获取输入的手机号和验证码
        String phone = loginVo.getPhone();
        String pwd = loginVo.getPwd();
        //判断手机号和验证码是否为空
        if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(pwd)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //判断是否是第一次登录，根据手机号查询数据库，如果不存在返回错误
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        UserInfo userInfo = baseMapper.selectOne(wrapper);
        if(userInfo==null){
            throw new YyghException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        //不是第一次，直接进行登录
        // 返回登录信息
        // 返回用户名
        String code = userInfo.getPwd();
        if(!code.equals(pwd)) {
            throw new YyghException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        Map<String,String> map=new HashMap<>();
        map.put("nickname",userInfo.getNickName());
        map.put("photo",userInfo.getPhoto()+"");
        map.put("point",userInfo.getPoint());
        return map;
    }

    @Override
    public boolean registerUser(RegisterVo registerVo,String checkCode) {
        UserInfo userInfo=new UserInfo();
        userInfo.setPhone(registerVo.getPhone());
        userInfo.setNickName(registerVo.getNickname());
        userInfo.setPwd(registerVo.getPwd());
        userInfo.setPhoto(registerVo.getPhoto());
        userInfo.setPoint(registerVo.getPoint());
        QueryWrapper<UserInfo>wrapper=new QueryWrapper<>();
        wrapper.eq("phone",registerVo.getPhone());

        if(baseMapper.selectOne(wrapper)!=null){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//        if(!checkCode.equals(redisTemplate.opsForValue().get(registerVo.getPhone()))){
//            return false;
//        }
        baseMapper.insert(userInfo);
        return true;
    }

    @Override
    public boolean changeName(String phone,String nickName) {
        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.eq("phone",phone);
        UserInfo user=baseMapper.selectOne(wrapper);
        if(user==null){
            throw new YyghException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
        user.setNickName(nickName);
        baseMapper.update(user,wrapper);
        return true;
    }

    @Override
    public boolean changePwd(String phone, String pwd,String checkCode) {
        QueryWrapper<UserInfo>wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        UserInfo userInfo=baseMapper.selectOne(wrapper);
        if(userInfo==null){
            return false;
        }
        if(!checkCode.equals(redisTemplate.opsForValue().get(phone))){
            return false;
        }
        userInfo.setPwd(pwd);
        baseMapper.update(userInfo,wrapper);
        return true;
    }

}
